package xcmg.device.service.warehouse;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.warehouse.WarehouseDealerAuthDO;
import xcmg.device.dao.mapper.warehouse.WarehouseDealerAuthDOMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.warehouse.WarehouseDealerAuthVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户授权信息Service
 * @Author huhuawei
 */
@Service
public class WarehouseDealerAuthService {

    @Autowired
    private WarehouseDealerAuthDOMapper warehouseDealerAuthDOMapper;

    @Autowired
    private FastGenClient fastGenClient;

    /**
     * 增加客户授权信息
     * @param listVO
     * @return
     */
    public ErrorCode addWarehouseDealerAuth(List<WarehouseDealerAuthVO>  listVO){
        //循环遍历客户授权信息
        for (WarehouseDealerAuthVO vo : listVO){
            //checked：true 如果存在 不做操作，如果不存在插入
            int cnt = warehouseDealerAuthDOMapper.countByDealerId(vo.getDealerId(),vo.getDealerAuthId(),vo.getSupplierId());
            if(vo.getChecked()){
                //根据客户ID和客户授权ID查询是否已存在授权信息
                if(cnt <= 0){
                   //插入
                    ActionResult<String> textResult = fastGenClient.textGuid();
                    //生成id
                    vo.setId(textResult.getValue().toString());
                    warehouseDealerAuthDOMapper.insert(vo);
                }
            }else{
                //checked:false 检查是否讯在：已存在的话删除
                if(cnt > 0){
                   //删除
                    warehouseDealerAuthDOMapper.deleteByDealerInfo(vo.getDealerId(),vo.getDealerAuthId(),vo.getSupplierId());
                }
            }
        }
        return ErrorCode.Success;
    }

    /**
     * 分页查询客户授权信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageCommonVO listWarehousesDealerAuth(int pageNum, int pageSize,WarehouseDealerAuthVO warehouseDealerAuthVO) {
        //默认按照id排序
        PageHelperPlus.orderBy("ID");
        PageHelperPlus.startPage(pageNum, pageSize);
        //供应商信息不能不能为空
        if (warehouseDealerAuthVO.getSupplierId() == null
                || warehouseDealerAuthVO.getSupplierId().isEmpty()) {
            return new PageCommonVO();
        }
        List<WarehouseDealerAuthDO> lists = warehouseDealerAuthDOMapper.list(warehouseDealerAuthVO);
        List<WarehouseDealerAuthVO> warehouseVOS = new ArrayList<>();
        for (WarehouseDealerAuthDO warehouseDealerAuthDO1 : lists) {
            WarehouseDealerAuthVO warehouseDealerAuthVO1 = new WarehouseDealerAuthVO();
            BeanUtils.copyProperties(warehouseDealerAuthDO1, warehouseDealerAuthVO1);
            warehouseVOS.add(warehouseDealerAuthVO1);
        }
        PageCommonVO pageCommonVO =new PageCommonVO();
        pageCommonVO.setPageInfo(new PageInfo(lists));
        pageCommonVO.setPageInfoList(warehouseVOS);

        return pageCommonVO;
    }
    /*public PageCommonVO list(SearchCommonVO<CompanySupplierVO> condition)
    {
        PageCommonVO pageCommonVO = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<CompanySupplierDO> doList = companySupplierMapper.list(condition.getFilters());
        List<CompanySupplierVO> voList = new ArrayList<>();
        for (CompanySupplierDO companySupplierDO : doList)
        {
            if (companySupplierDO == null)
            {
                continue;
            }
            voList.add(getVO(companySupplierDO));
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }*/
}
